from flask import request, jsonify, Blueprint
from app.models.system_log import SystemLog

system_bp = Blueprint('system', __name__, url_prefix='/api/system')
@system_bp.route('/logs', methods=['GET'])
def get_system_logs():
        # 获取查询参数
        page = request.args.get('page', default=1, type=int)
        page_size = request.args.get('pageSize', default=10, type=int)
        keyword = request.args.get('keyword', default='', type=str)
        level = request.args.get('level', default='', type=str)

        # 构建查询条件
        query = SystemLog.query
        if keyword:
            query = query.filter(SystemLog.message.contains(keyword))
        if level:
            query = query.filter(SystemLog.level == level)

        # 分页查询
        pagination = query.paginate(page=page, per_page=page_size, error_out=False)
        logs = pagination.items
        total = pagination.total

        # 返回结果
        return jsonify({
            'code': 200,
            'data': {
                'records': [log.to_dict() for log in logs],
                'total': total
            }
        })